To analyze the stock market, we will collect the stock price data of Google.¶

Let’s start by collecting the stock price data of Google. here we will use the yfinance API of Yahoo Finance for such¶
In [1]:
pip install yfinance
Requirement already satisfied: yfinance in c:\users\harik\anaconda3\lib\site-packages (0.1.77)
Requirement already satisfied: lxml>=4.5.1 in c:\users\harik\anaconda3\lib\site-packages (from yfinance) (4.8.0)
Requirement already satisfied: numpy>=1.15 in c:\users\harik\anaconda3\lib\site-packages (from yfinance) (1.21.5)
Requirement already satisfied: multitasking>=0.0.7 in c:\users\harik\anaconda3\lib\site-packages (from yfinance) (0.0.11)
Requirement already satisfied: appdirs>=1.4.4 in c:\users\harik\anaconda3\lib\site-packages (from yfinance) (1.4.4)
Requirement already satisfied: pandas>=0.24.0 in c:\users\harik\anaconda3\lib\site-packages (from yfinance) (1.3.3)
Requirement already satisfied: requests>=2.26 in c:\users\harik\anaconda3\lib\site-packages (from yfinance) (2.27.1)
Requirement already satisfied: pytz>=2017.3 in c:\users\harik\anaconda3\lib\site-packages (from pandas>=0.24.0->yfinance) (2021.3)
Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\harik\anaconda3\lib\site-packages (from pandas>=0.24.0->yfinance) (2.8.2)
Requirement already satisfied: six>=1.5 in c:\users\harik\anaconda3\lib\site-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.16.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\harik\anaconda3\lib\site-packages (from requests>=2.26->yfinance) (1.26.9)
Requirement already satisfied: idna<4,>=2.5 in c:\users\harik\anaconda3\lib\site-packages (from requests>=2.26->yfinance) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\harik\anaconda3\lib\site-packages (from requests>=2.26->yfinance) (2021.10.8)
Requirement already satisfied: charset-normalizer~=2.0.0 in c:\users\harik\anaconda3\lib\site-packages (from requests>=2.26->yfinance) (2.0.4)
Note: you may need to restart the kernel to use updated packages.
In [1]:
#import libraries
import pandas as pd
import numpy as np
import yfinance as yf
import datetime
from datetime import date, timedelta
import plotly.graph_objects as go
import plotly.express as px
In [2]:
#Establishing dates
today = date.today()
d1 = today.strftime("%Y-%m-%d")
end_date = d1
d2 = date.today() - timedelta(days=365)
d2 = d2.strftime("%Y-%m-%d")
start_date =d2
In [3]:
#Get data for stock by google. 
data = yf.download('GOOG', start=start_date, end=end_date, progress=False)
data["Date"] =data.index
data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]]
data.reset_index(drop=True, inplace=True)
data.head()
Out[3]:
Date Open High Low Close Adj Close Volume
0 2021-10-12 00:00:00-04:00 139.637497 139.695007 136.250000 136.712997 136.712997 22536000
1 2021-10-13 00:00:00-04:00 137.750000 138.550003 136.976501 137.899994 137.899994 16380000
2 2021-10-14 00:00:00-04:00 139.951996 141.651505 139.339005 141.412003 141.412003 21426000
3 2021-10-15 00:00:00-04:00 142.199997 142.199997 141.064499 141.675003 141.675003 21250000
4 2021-10-18 00:00:00-04:00 141.213501 142.998749 141.213501 142.960495 142.960495 16564000
In [4]:
#Analyse the pirce movement of stock by candlestick chart
figure = go.Figure(data=[go.Candlestick(x=data["Date"], open=data["Open"],
                                        high=data["High"], low=data["High"],close=data["Close"])])
figure.update_layout(title="Google Stock Price Analysis", xaxis_rangeslider_visible=False)
figure.show()
In [5]:
# Box plot to visualize the analyse Stock market
figure = px.bar(data, x="Date", y="Close")
figure.show()
In [6]:
# Employing range slider to analyse between two specific points
figure = px.line(data, x="Date", y="Close", title="Stock Market Analysis using Rangeslider")
figure.update_xaxes(rangeslider_visible=True)
figure.show()
In [7]:
#Using time period selector for an interactive approach
figure = px.line(data, x='Date', y='Close',title='Stock Market Analysis with Time Period Selectors')

figure.update_xaxes(rangeselector=dict(buttons=list([dict(count=1, label="1m", step="month", stepmode="backward"),
                                                     dict(count=6, label="6m", step="month", stepmode="backward"),
                                                     dict(count=3, label="3m", step="month", stepmode="backward"),
                                                     dict(count=1, label="1y", step="year", stepmode="backward"),
                                                     dict(step="all")])))
figure.show()
In [10]:
#Removing weekends to better identfy trends
figure = px.scatter(data, x='Date', y='Close', range_x=['2021-07-12', '2022-07-11'],
                    title="Stock Market Analysis by Hiding Weekend Gaps")
figure.update_xaxes(rangebreaks=[dict(bounds=["sat", "sun"])])
figure.show()
In [ ]: